﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPortDemo
{
    public partial class Form1 : Form
    {
        private bool isOpen = false;
        private SerialPort mySerialPort = null; // = new SerialPort("COM3"); // 选择正确的串口号

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonOpenOrClose_Click(object sender, EventArgs e)
        {
            isOpen = !isOpen;
            if (isOpen)
            {
                buttonOpenOrClose.Text = "关闭串口";
                //打开串口
                OpenSerialPort();
            }
            else
            {
                buttonOpenOrClose.Text = "打开串口";
                // 关闭串口
                CloseSerialPort();
            }

        }

        private void OpenSerialPort()
        {
            try
            {
                mySerialPort = new SerialPort(comboBoxCom.Text); // 选择正确的串口号

                mySerialPort.BaudRate = Convert.ToInt32(comboBoxBaudrate.Text);
                mySerialPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParityCheck.Text);
                mySerialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBit.Text);
                mySerialPort.DataBits = Convert.ToInt32(comboBoxDataBit.Text);
                mySerialPort.Handshake = Handshake.None;
                mySerialPort.RtsEnable = true;

                mySerialPort.Open();

                richTextBoxLog.AppendText($"打开串口{comboBoxCom.Text}成功\n");

            }
            catch (Exception ex)
            {
                richTextBoxLog.AppendText($"打开串口{comboBoxCom.Text}失败，{ex.Message}\n");
            }

        }

        private void CloseSerialPort()
        {
            try
            {
                mySerialPort.Close();

                richTextBoxLog.AppendText($"关闭串口{comboBoxCom.Text}成功\n");

            }
            catch (Exception ex)
            {
                richTextBoxLog.AppendText($"关闭串口{comboBoxCom.Text}失败，{ex.Message}\n");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 获取当前所有可用的串口名称
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBoxCom.Items.Add(port);
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            try
            {           
                // 串口打开后，可以进行读写操作
                mySerialPort.Write(richTextBoxData.Text);
                richTextBoxLog.AppendText($"写串口{comboBoxCom.Text}成功，数据：{richTextBoxData.Text}\n");
            }
            catch (Exception ex)
            {
                richTextBoxLog.AppendText($"写串口{comboBoxCom.Text}失败，{ex.Message}\n");
            }

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            richTextBoxData.Clear();
        }
    }
}
